package cn.cloudreal.cbms.castDetail.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.castDetail.dao.CastDetailDao;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.vo.CastDetail;
import cn.cloudreal.cbms.vo.Material;
import cn.cloudreal.cbms.vo.MediaPrice;
import cn.cloudreal.cbms.vo.Mediasumm;
import cn.cloudreal.cbms.vo.Medium;
import cn.cloudreal.cbms.vo.Project;
import cn.cloudreal.cbms.vo.ProjectMedium;
import cn.cloudreal.cbms.vo.QueryArrangement;

/**
 * <p>
 * Title: CBM
 * </p>
 * <p>
 * Description:投放明细Dao层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author WangTao
 * @date 2012-07-09
 */
public class CastDetailDaoImpl extends SqlMapClientDaoSupport implements
        CastDetailDao
{

    private final String GET_CAST_DETAIL_COUNT = "castdetail.getCastDetailCount";
    private final String GET_MEDIUM_CAST_DETAIL_LIST = "castdetail.getMediumCastDetailList";
    private final String GET_MEDIUM_CAST_DETAIL = "castdetail.getCastDetail";

    private final String UPDATE_BASIC_INFO = "castdetail.updateBasicInfo";
    private final String UPDATE_MATERIAL_INFO = "castdetail.updateMaterialInfo";

    private final String GET_PROJECT_CAST_DETAIL_LIST = "castdetail.getProjectCastDetailList";
    private final String DELETE_CAST_DETAIL = "castdetail.deleteCastDetail";
    private final String INSERT_BASIC_INFO = "castdetail.insertBasicInfo";
    private final String AJAX_GET_PROJECT = "castdetail.ajaxGetProject";
    private final String AJAX_GET_MEDIA = "castdetail.ajaxGetMedia";
    private final String AJAX_GET_MEDIUM = "castdetail.ajaxGetMedium";
    private final String INSERT_MATERIAL_INFO = "castdetail.insertMaterialInfo";
    private final String AJAX_GET_MATERIAL = "castdetail.ajaxGetMaterial";
    private final String GET_PROJECT_MEDIUM = "castdetail.getProjectMedium";
    private final String GET_PROJECT = "castdetail.getProject";
    private final String GET_MEDIUM = "castdetail.getMedium";
    private final String QUERY_ARRANGEMENT = "castdetail.queryArrangement";
    private final String QUERY_ARRANGEMENT_COUNT = "castdetail.queryArrangementCount";
    private final String GET_MEDIUM_BY_FIELD = "castdetail.getMediumByField";

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(CastDetailDaoImpl.class);

    /**
     * 获取投放明细count
     * 
     * @return int
     */
    public int getCastDetailCount(Map condition)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getMediumCastDetailCount() method, return int");
        }

        // 初始化
        int count = 0;
        // 查询
        count = (Integer) getSqlMapClientTemplate().queryForObject(GET_CAST_DETAIL_COUNT,
                condition);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getMediumCastDetailCount() method, return int");
        }
        return count;
    }

    /**
     * 获取媒介投放明细列表
     * 
     * @return List<CastDetail>
     */
    public List<CastDetail> getMediumCastDetailList(Map condition, PageBean page)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getMediumCastDetailList() method, return List<CastDetail>");
        }

        // 初始化
        List<CastDetail> castDetails = null;
        // 总记录数不为零才查询
        if (page.getTotalRecords() != 0)
        {
            // 查询
            castDetails = getSqlMapClientTemplate().queryForList(GET_MEDIUM_CAST_DETAIL_LIST,
                    condition,
                    (page.getPageNo() - 1) * page.getPageSize(),
                    page.getPageSize());
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getMediumCastDetailList() method, return List<CastDetail>");
        }
        return castDetails;
    }

    /**
     * 获取项目投放明细列表
     * 
     * @return List<CastDetail>
     */
    public List<CastDetail> getProjectCastDetailList(Map condition,
            PageBean page)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getProjectCastDetailList() method, return List<CastDetail>");
        }

        // 初始化
        List<CastDetail> castDetails = null;
        // 总记录数不为零才查询
        if (page.getTotalRecords() != 0)
        {
            // 查询
            castDetails = getSqlMapClientTemplate().queryForList(GET_PROJECT_CAST_DETAIL_LIST,
                    condition,
                    (page.getPageNo() - 1) * page.getPageSize(),
                    page.getPageSize());
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getProjectCastDetailList() method, return List<CastDetail>");
        }
        return castDetails;
    }

    /**
     * 删除投放明细
     * 
     * @return void
     */
    public boolean deleteCastDetail(ProjectMedium pm, Medium m)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl deleteCastDetail() method, return void");
        }
        boolean flag = false;
        //
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());

        m.setContractYear(time);
        //
        int i = getSqlMapClientTemplate().update("castdetail.updateTotalArrangement",
                m);
        if (i == 1)
        {
            getSqlMapClientTemplate().delete(DELETE_CAST_DETAIL,
                    pm.getProMedID());
            flag = true;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl deleteCastDetail() method, return void");
        }
        return flag;
    }

    /**
     * 插入
     * 
     * @return int
     */
    public int insertBasicInfo(ProjectMedium projectMedium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl insertBasicInfo() method, return int");
        }
        projectMedium.setArrangeTime(projectMedium.getArrangeTime() + "-01");

        // int count = (Integer)
        // getSqlMapClientTemplate().queryForObject("castdetail.testInsertBasicInfo",
        // projectMedium);
        int count = (Integer) getSqlMapClientTemplate().queryForObject("castdetail.testInsertBasicInfo",
                projectMedium);
        int id = -9527;
        if (count == 0)
        {
            // 日历
            Calendar c = Calendar.getInstance();
            c.clear();
            // 设置年度
            c.set(Calendar.YEAR, (new Date().getYear() + 1900));
            // 初始化
            StringBuffer sb = new StringBuffer();
            // 循环
            for (int i = 0; i < c.getActualMaximum(Calendar.DAY_OF_YEAR); i++)
            {
                sb.append("," + 0);
            }
            //
            // projectMedium.setArrangeTime(projectMedium.getArrangeTime() +
            // "-01");
            //
            projectMedium.setMediumArrangement(sb.toString().substring(1));
            //
            projectMedium.setMediumAmount(0);
            // 总记录数不为零才查询
            id = (Integer) getSqlMapClientTemplate().insert(INSERT_BASIC_INFO,
                    projectMedium);
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl insertBasicInfo() method, return int");
        }
        return id;
    }

    /**
     * 媒体查询
     * 
     * @return List<Mediasumm>
     */
    public List<Mediasumm> ajaxGetMedia(MediaPrice mediaPrice)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl ajaxGetMedia() method, return List<Mediasumm>");
        }
        mediaPrice.setMediaStartTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        //
        List<Mediasumm> ajaxMedias = getSqlMapClientTemplate().queryForList(AJAX_GET_MEDIA,
                mediaPrice);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl ajaxGetMedia() method, return List<Mediasumm>");
        }
        return ajaxMedias;
    }

    /**
     * 媒介查询
     * 
     * @return List<Medium>
     */
    public List<Medium> ajaxGetMedium(Medium medium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl ajaxGetMedium() method, return List<Medium>");
        }

        List<Medium> ajaxMediums = getSqlMapClientTemplate().queryForList(AJAX_GET_MEDIUM,
                medium);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl ajaxGetMedium() method, return List<Medium>");
        }
        return ajaxMediums;
    }

    /**
     * 项目查询
     * 
     * @return List<Project>
     */
    public List<Project> ajaxGetProject(Project project)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl ajaxGetProject() method, return List<Project>");
        }

        List<Project> ajaxProjects = getSqlMapClientTemplate().queryForList(AJAX_GET_PROJECT,
                project);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl ajaxGetProject() method, return List<Project>");
        }
        return ajaxProjects;
    }

    public void insertMaterialInfo(ProjectMedium projectMedium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl insertMaterialInfo() method, return void");
        }

        if (!"".equals(projectMedium.getId_material()))
        {
            projectMedium.setId_material("," + projectMedium.getId_material()
                    + ",");
        }
        getSqlMapClientTemplate().update(INSERT_MATERIAL_INFO, projectMedium);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl insertMaterialInfo() method, return void");
        }
    }

    /**
     * 物料查询
     * 
     * @return List<Material>
     */
    public List<Material> ajaxGetMaterial(Material material)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl ajaxGetMaterial() method, return List<Material>");
        }
        if (material.getMaterialID() != null
                && !"".equals(material.getMaterialID()))
        {
            if (material.getMaterialID().length() > 2)
            {
                material.setMaterialID(material.getMaterialID().substring(1,
                        (material.getMaterialID().length() - 1)));
            }
            else
            {
                material.setMaterialID("");
            }
        }
        //
        List<Material> materials = getSqlMapClientTemplate().queryForList(AJAX_GET_MATERIAL,
                material);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl ajaxGetMaterial() method, return List<Material>");
        }
        return materials;
    }

    /**
     * 按编号查询
     */
    public Medium getMediumByMediumID(int mediumID)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getMediumByMediumID() method, return Medium");
        }

        Medium medium = (Medium) getSqlMapClientTemplate().queryForObject(GET_MEDIUM,
                mediumID);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getMediumByMediumID() method, return Medium");
        }
        return medium;
    }

    /**
     * 按编号查询
     */
    public Project getProjectByProjectID(String projectID)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getProjectByProjectID() method, return Project");
        }

        Project project = (Project) getSqlMapClientTemplate().queryForObject(GET_PROJECT,
                projectID);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getProjectByProjectID() method, return Project");
        }
        return project;
    }

    /**
     * 按编号查询
     */
    public ProjectMedium getProjectMediumByProMedID(int proMedID)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getProjectMediumByProMedID() method, return ProjectMedium");
        }

        ProjectMedium projectMedium = (ProjectMedium) getSqlMapClientTemplate().queryForObject(GET_PROJECT_MEDIUM,
                proMedID);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getProjectMediumByProMedID() method, return ProjectMedium");
        }
        return projectMedium;
    }

    public boolean updateAllArrangement(String[] totalMediumArrangement,
            ProjectMedium pm,
            Medium m)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl updateAllArrangement() method, return boolean");
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < totalMediumArrangement.length; i++)
        {
            sb.append("," + totalMediumArrangement[i]);
        }
        m.setMediumArrangement(sb.toString().substring(1));

        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());

        m.setContractYear(time);

        int i = getSqlMapClientTemplate().update("castdetail.updateTotalArrangement",
                m);

        if (i == 1)
        {
            getSqlMapClientTemplate().update("castdetail.updateArrangement", pm);
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl updateAllArrangement() method, return boolean");
        }
        return false;
    }

    public CastDetail getMediumCastDetail(ProjectMedium projectMedium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getMediumCastDetail() method, return CastDetail");
        }

        CastDetail castDetail = (CastDetail) getSqlMapClientTemplate().queryForObject(GET_MEDIUM_CAST_DETAIL,
                projectMedium);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getMediumCastDetail() method, return CastDetail");
        }
        return castDetail;
    }

    public int updateMediumCastDetail(CastDetail castDetail, int flag)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl updateMediumCastDetail() method, return void");
        }
        int status = 0;
        switch (flag)
        {
            case 1:
                ProjectMedium pm = new ProjectMedium();
                pm.setProMedID(castDetail.getProMedID());
                CastDetail cd = (CastDetail) getSqlMapClientTemplate().queryForObject(GET_MEDIUM_CAST_DETAIL,
                        pm);
                pm.setProjectID(cd.getProjectID());
                pm.setArrangeTime(castDetail.getArrangeTime());
                pm.setId_medium(Integer.valueOf(cd.getId_medium()));
                int count = (Integer) getSqlMapClientTemplate().queryForObject("castdetail.testInsertBasicInfo",
                        pm);
                if (count == 0)
                {
                    getSqlMapClientTemplate().update(UPDATE_BASIC_INFO,
                            castDetail);
                }
                else
                {
                    status = 9527;
                }
                break;
            case 2:
                if (!"".equals(castDetail.getId_material()))
                {
                    castDetail.setId_material("," + castDetail.getId_material()
                            + ",");
                }
                getSqlMapClientTemplate().update(UPDATE_MATERIAL_INFO,
                        castDetail);
                break;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl updateMediumCastDetail() method, return void");
        }
        return status;
    }

    public List<QueryArrangement> queryArrangement(Map condition, PageBean page)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl queryArrangement() method, return int");
        }
        List<QueryArrangement> arrangements = new ArrayList<QueryArrangement>();
        //
        if ("exportAll".equals(condition.get("exportAll")))
        {
            // 查询
            arrangements = getSqlMapClientTemplate().queryForList(QUERY_ARRANGEMENT,
                    condition);
        }
        else
        {
            // 查询
            arrangements = getSqlMapClientTemplate().queryForList(QUERY_ARRANGEMENT,
                    condition,
                    (page.getPageNo() - 1) * page.getPageSize(),
                    page.getPageSize());
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl queryArrangement() method, return int");
        }
        return arrangements;
    }

    public int queryArrangementCount(Map condition)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl queryArrangement() method, return int");
        }
        //
        int count = 0;
        // 查询
        count = (Integer) getSqlMapClientTemplate().queryForObject(QUERY_ARRANGEMENT_COUNT,
                condition);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl queryArrangement() method, return int");
        }
        return count;
    }

    public List<String> getMediumByField(Map condition)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailDaoImpl getMediumByField() method, return List<String>");
        }
        //
        // int count = 0;
        // // 查询
        // count = (Integer)
        // getSqlMapClientTemplate().queryForObject(QUERY_ARRANGEMENT_COUNT,
        // condition);
        String[] fields = condition.get("dbField").toString().split(",");
        //
        Map strings = (Map) getSqlMapClientTemplate().queryForObject(GET_MEDIUM_BY_FIELD,
                condition);
        //
        List<String> mediumField = new ArrayList<String>();

        for (int i = 0; i < fields.length; i++)
        {
            if (strings.get(fields[i].replaceFirst("d.", "")) != null)
            {
                mediumField.add(strings.get(fields[i].replaceFirst("d.", ""))
                        + "");
            }
            else
            {
                mediumField.add("");
            }
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailDaoImpl getMediumByField() method, return List<String>");
        }
        return mediumField;
    }

}
